package com.sjtu.rest;

import com.sjtu.dao.model.ClientInfo;
import com.sjtu.dao.model.JobRunHistory;
import com.sjtu.dao.repository.ClientInfoRepository;
import com.sjtu.dao.repository.JobRunHistoryRepository;
import com.sjtu.dao.repository.TaskInfoRepository;
import com.sjtu.feign.JobRunHistoryFeign;
import com.sjtu.service.JobRerunService;
import com.sjtu.web.BaseRest;
import com.sjtu.web.Result;
import com.sjtu.web.form.JobRerunForm;
import com.sjtu.web.search.JobRunHistorySearch;
import com.sjtu.web.vo.JobRunHistoryVo;
import com.sjtu.web.vo.TaskInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务运行历史
 * nerine
 * 1/29/2020
 */
@RequestMapping(JobRunHistoryFeign.PREFIX)
@RestController
public class JobRunHistoryRest extends BaseRest {
    @Autowired
    private ClientInfoRepository clientInfoRepository;
    @Autowired
    private TaskInfoRepository taskInfoRepository;
    @Autowired
    private JobRunHistoryRepository jobRunHistoryRepository;
    @Autowired
    private JobRerunService jobRerunService;

    @PostMapping("/list")
    public Result<?> list(@RequestBody JobRunHistorySearch search) {
        ClientInfo clientInfo = null;
        if(StringUtils.isNoneBlank(search.getSystemId())) {
            clientInfo = new ClientInfo();
            clientInfo.setSystemId(search.getSystemId());
            clientInfo = clientInfoRepository.findOne(Example.of(clientInfo)).get();
        }
        JobRunHistory query = new JobRunHistory();
        query.setClientInfo(clientInfo);
        Page<JobRunHistory> result = jobRunHistoryRepository.findAll(
                Example.of(query),
                PageRequest.of(
                        search.getCurrent(), search.getSize(),
                Sort.by(Sort.Direction.DESC, "createdDate")
                )
        );
        List<JobRunHistoryVo> list = result.stream().map(e -> {
            TaskInfoVo taskInfoVo = TaskInfoRest.createTaskInfoVo(e.getTaskInfo());
            JobRunHistoryVo vo = new JobRunHistoryVo();
            vo.setTaskInfo(taskInfoVo);
            vo.setClientInfo(taskInfoVo.getClientInfo());
            vo.setIdJobRunHistory(e.getIdJobRunHistory());
            vo.setCommonRunId(e.getCommonRunId());
            vo.setInstance(e.getInstance());
            vo.setRemark(e.getRemark());
            vo.setRunBeginTime(e.getRunBeginTime());
            vo.setRunEndTime(e.getRunEndTime());
            vo.setStatus(e.getStatus());
            return vo;
        }).collect(Collectors.toList());
        return success(list);
    }

    @PostMapping("/rerun")
    public Result<?> rerun(@RequestBody JobRerunForm form) {
        JobRunHistory query = new JobRunHistory();
        query.setIdJobRunHistory(form.getRunId());
        JobRunHistory runHistory = jobRunHistoryRepository.findOne(Example.of(query)).get();
        jobRerunService.rerun(runHistory);
        return success();
    }
}
